package com.kobi.controller;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.kobi.data.Book;

import com.kobi.connection.*;


@WebServlet("/queryallBook")
public class queryallBook extends HttpServlet {
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs=null;
        List<Book> list=new ArrayList<>();
        try {
            conn = JDBCUtils.getConnection();
            String sql = "select * from books";
            pstmt = conn.prepareStatement(sql);
            rs= pstmt.executeQuery();
            while(rs.next()) {
                Book b=new Book();
                b.setBookid(rs.getString(1));
                b.setTitle(rs.getString(2));
                b.setAuthor(rs.getString(3));
                b.setPublisher(rs.getString(4));
                b.setPrice(rs.getDouble(5));
                list.add(b);
            }
            request.setAttribute("list", list);
            request.getRequestDispatcher("/showall.jsp").forward(request, response);
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            //6. 释放资源
            JDBCUtils.close(rs,pstmt,conn);
        }
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

}
